<?php
/**
 * 出租房
 * User: E 房cms www.fangcms.com
 * Date: 2015/1/20
 * Time: 10:51
 */
namespace Home\Controller;
use Org\Yike\FangCms;
class EstateController extends CommonController{
    public function _initialize(){
        parent::_initialize();
        $this->action = strtolower(ACTION_NAME);
    }

    public function index(){
        $FangCms = new FangCms();
        $menu = getLinkMenuFor(array(2,8,21),1);
        $houseArr = $FangCms->getEstateSearch($menu,true);
        $this->rec_used = $FangCms->getRecUsed(8);
        $this->rec_rental = $FangCms->getRecRental(8);
        $this->estate_type = cateFor($menu[2]);
        //购房知识
        $this->news_list = $FangCms::getIdsArticle(14);

        $this->area_cate = $houseArr['arr']['area_cate'];
        $this->cate_pid_info = $houseArr['arr']['cate_pid_info'];
        $this->cate_pid_list = $houseArr['arr']['cate_pid_list'];
        $this->menu = $menu;
        $this->url = $houseArr['url'];
        $this->s = $houseArr['s'];
        $this->list = $houseArr['arr']['list'];
        $this->page = $houseArr['arr']['page'];
        $this->houseArr = $houseArr;
        $seo = $houseArr['arr']['cate_info'];
        if($this->cate_pid_info  or $seo){
            if($this->cate_pid_info['id']!=$seo['id']){
                $name =  $this->cate_pid_info['name'].$seo['name'];
            }else{
                $name = $seo['name'];
            }
            $seo['seo_title'] = $seo['seo_keys'] = $name."小区大全,".$name."小区名称,房价,出售出租";
            $seo['seo_desc'] = $this->siteName.$name."免费提供所有".$name."小区大全以及".$name."小区名称、价格变动、房源信息等,查找".$name."小区房源";
            $this->seoKey($seo,'name');
        }
        $this->display();
    }

    //小区主页
    public function detail(){
        $resArr = $this->getEstate();
        $rental = $resArr['rental'];
        $this->menu = getLinkMenuFor(array(10,17,21,20));
        $this->map = explode(',',$rental['map']);
        $this->data = unserialize($rental['data']);
        $this->img_list = json_decode($rental['file'],true);
        //本小区二手房
        $where = array('status'=>1,'estate_id'=>$resArr['rental']['id']);
        $this->used_list = M('used')->where($where)->order('id desc')->limit(5)->select();
        //本小区出租房
        $this->rental_list = M('rental')->where($where)->order('id desc')->limit(5)->select();
        //同区域小区
        $this->area_list = M('estate')->where(array('status'=>1,'area_id'=>$rental['area_id']))->order('id desc')->limit(10)->select();
        $this->seoKey($resArr['rental']);
        $this->display();
    }

    //二手房
    public function used(){
        $info = $this->getEstate();
        $FangCms = new FangCms();
        $menu = getLinkMenuFor(array(2,8,10,17,20,21,22,23,24),1);
        $houseArr = $FangCms->getEstateIdUsedSearch($menu,true);
        $this->rec_used = $FangCms->getRecUsed(8);

        $this->menu = $menu;
        $this->url = $houseArr['url'];
        $this->s = $houseArr['s'];
        $this->list = $houseArr['arr']['list'];
        $this->page = $houseArr['arr']['page'];
        $this->houseArr = $houseArr;
        $this->news_list = $FangCms->getIdsArticle(15);
        $this->seoKey(array(
            'seo_title'=>$info['rental']['title'].'二手房'
        ));
        $this->display();
    }

    //出租房
    public function rental(){
        $info = $this->getEstate();
        $FangCms = new FangCms();
        $menu = getLinkMenuFor(array(2,8,10,17,20,21,27,26,25,23,24),1);
        $houseArr = $FangCms->getEstateIdRentalSearch($menu,true);
        $this->rec_used = $FangCms->getRecUsed(8);

        $this->menu = $menu;
        $this->url = $houseArr['url'];
        $this->s = $houseArr['s'];
        $this->list = $houseArr['arr']['list'];
        $this->page = $houseArr['arr']['page'];
        $this->houseArr = $houseArr;
        $this->news_list = $FangCms->getIdsArticle(17);
        $this->seoKey(array(
            'seo_title'=>$info['rental']['title'].'出租房'
        ));
        $this->display();
    }


    //小区图片
    public function pic(){
        $info = $this->getEstate();
        $this->img_list = json_decode($info['rental']['file'],true);
        $this->seoKey(array(
            'seo_title'=>$info['rental']['title'].'图片,'.$info['rental']['title'].'相册'
        ));
        $this->display();
    }

    //小区详细
    public function info(){
        $resArr = $this->getEstate();
        $this->data = unserialize($resArr['rental']['data']);
        //同区域小区
        $this->area_list = M('estate')->where(array('status'=>1,'area_id'=>$resArr['rental']['area_id']))->order('id desc')->limit(10)->select();
        //本小区二手房
        $where = array('status'=>1,'estate_id'=>$resArr['rental']['id']);
        $this->used_list = M('used')->where($where)->order('id desc')->limit(10)->select();
        //本小区出租房
        $this->rental_list = M('rental')->where($where)->order('id desc')->limit(10)->select();

        $this->seoKey(array(
            'seo_title'=>$resArr['rental']['title'].'小区详细,'.$resArr['rental']['title'].'小区介绍'
        ));
        $this->display();
    }

    private function getEstate(){
        $estate_id = intval($_GET['hid']) ? intval($_GET['hid']) : intval($_GET['id']);
        if(!$estate_id){
            $this->error('参数不正确',U('Estate/index'));
        }
        $estate = M('estate')->where(array('id'=>$estate_id))->find();
        if(!$estate['id']){
            $this->error('找不到对应的信息',U('Rental/index'));
        }
        //求和小区中的信息
        $sell = M('used')->where(array('estate_id'=>$estate_id,'status'=>1))->count();
        $lease = M('rental')->where(array('estate_id'=>$estate_id,'status'=>1))->count();
        M('estate')->where(array('id'=>$estate_id))->save(array('sell_count'=>$sell,'lease_count'=>$lease));

        //给楼盘增加浏览次数
        $FangCms = new FangCms();
        $FangCms->setInc($estate['id'],'estate');

        //当前位置
        $this->nav = $FangCms->currentLocation('area_cate',array('status'=>1,'id'=>$estate['area_id']));
        $estate['alias'] = M('area_cate')->where(array('id'=>$estate_id))->getField('alias');
        $this->estate = $estate;
        return array('rental'=>$estate);
    }
}